package com.text1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Del {
    public static void main(String[] args) throws SQLException {
         Del del = new Del();
        int count= del.deleteById(7);
        if(count>0){
            System.out.println("删除成功");
        }

    }
    public  int deleteById(int id) throws SQLException {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        int row=0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/kgcnews";
            String username="root";
            String password="123456";
            connection= DriverManager.getConnection(url,username,password);

            String sql="DELETE FROM news_category WHERE id=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            row= preparedStatement.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            preparedStatement.close();
            connection.close();
        }
        return row;
    }
}
